package com.zhiliang.waibao.myapplication.ui;

import android.Manifest;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhiliang.waibao.myapplication.R;
import com.zhiliang.waibao.myapplication.component.ApplicationComponent;
import com.zhiliang.waibao.myapplication.ui.base.AppManager;
import com.zhiliang.waibao.myapplication.ui.base.BaseActivity;
import com.zhiliang.waibao.myapplication.ui.base.SupportFragment;
import com.zhiliang.waibao.myapplication.utils.StatusBarUtil;
import com.zhiliang.waibao.myapplication.utils.T;
import com.zhiliang.waibao.myapplication.widget.BottomBar;
import com.zhiliang.waibao.myapplication.widget.BottomBarTab;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    SupportFragment[] mFragments = new SupportFragment[3];
    @BindView(R.id.bottomBar)
    BottomBar bottomBar;
    @BindView(R.id.contentContainer)
    FrameLayout contentContainer;


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private long exitTime = 0;

    //退出app
    private void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            AppManager.getAppManager().finishAllActivity();
            System.exit(0);
        }
    }

    @Override
    public void onRetry() {

    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_main;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {

    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        // fragment 头部是 ImageView 的设置状态栏透明
        StatusBarUtil.hideFakeStatusBarView(MainActivity.this);
        if (savedInstanceState == null) {
            mFragments[0] = AFragment.newInstance();
            mFragments[1] = BFragment.newInstance();
            mFragments[2] = CFragment.newInstance();


            getSupportDelegate().loadMultipleRootFragment(R.id.contentContainer, 0,
                    mFragments[0],
                    mFragments[1],
                    mFragments[2]);
        } else {
            mFragments[0] = findFragment(AFragment.class);
            mFragments[1] = findFragment(BFragment.class);
            mFragments[2] = findFragment(CFragment.class);
        }
        bottomBar.addItem(new BottomBarTab(this, R.drawable.ic_main, "A"))
                .addItem(new BottomBarTab(this, R.drawable.ic_main, "B"))
                .addItem(new BottomBarTab(this, R.drawable.ic_main, "C"));


        bottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                getSupportDelegate().showHideFragment(mFragments[position], mFragments[prePosition]);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    @Override
    public void initData() {
        final RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions
                .request(Manifest.permission.CAMERA,
                        Manifest.permission.READ_PHONE_STATE)
                .subscribe(granted -> {
                    if (granted) {
                        T.showShort(MainActivity.this,"同意授权！");
                    } else {
                        T.showShort(MainActivity.this,"拒绝授权！");
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
